<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" :visible.sync="visible" :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules">
            <el-form-item label="会员" :label-width="formLabelWidth" prop="memberId">
                <fortress-select
                        v-model="form.memberId"
                        :dictList="memberIdList"
                        placeholder="请选择会员"
                        clearable
                ></fortress-select>
            </el-form-item>
            <el-form-item label="优惠券" :label-width="formLabelWidth" prop="couponId">
                <fortress-select
                        v-model="form.couponId"
                        :dictList="couponIdList"
                        placeholder="请选择优惠券"
                        clearable
                ></fortress-select>
            </el-form-item>
            <el-form-item label="获得方式" :label-width="formLabelWidth" prop="getType">
                <fortress-select
                        v-model="form.getType"
                        :dictList="CouponGetType_ordersList"
                        placeholder="请选择获得方式"
                        clearable
                ></fortress-select>
            </el-form-item>
            <el-form-item label="使用时间" :label-width="formLabelWidth" prop="useTime">
                <el-date-picker
                        v-model="form.useTime"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item label="获取时间" :label-width="formLabelWidth" prop="getTime">
                <el-date-picker
                        v-model="form.getTime"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item label="订单编号" :label-width="formLabelWidth" prop="orderSn">
                <el-input
                        v-model="form.orderSn"
                        autocomplete="off"
                        placeholder="请输入订单编号"
                ></el-input>
            </el-form-item>
            <el-form-item label="生效时间(什么时候生效)" :label-width="formLabelWidth" prop="effectiveTime">
                <el-date-picker
                        v-model="form.effectiveTime"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item label="到期时间" :label-width="formLabelWidth" prop="overdueTime">
                <el-date-picker
                        v-model="form.overdueTime"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
            </el-form-item>
            <el-form-item label="状态" :label-width="formLabelWidth" prop="status">
                <fortress-select
                        v-model="form.status"
                        :dictList="CouponStatus_ordersList"
                        placeholder="请选择状态"
                        clearable
                ></fortress-select>
            </el-form-item>
            <el-form-item label="支付类型" :label-width="formLabelWidth" prop="payType">
                <fortress-select
                        v-model="form.payType"
                        :dictList="CouponPayType_ordersList"
                        placeholder="请选择支付类型"
                        clearable
                ></fortress-select>
            </el-form-item>
            <el-form-item label="支付余额数量(弃用)" :label-width="formLabelWidth" prop="payNumber">
              <el-input-number
                  v-model="form.payNumber"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="支付积分数量" :label-width="formLabelWidth" prop="pointNumber">
              <el-input-number
                  v-model="form.pointNumber"
              ></el-input-number>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" >
            <el-button @click="visible = false" size="medium" type="danger">取 消</el-button>
            <el-button type="primary" size="medium" @click="submitForm('form')">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
    import { salecoupondataInfo, salecoupondataSave, salecoupondataEdit } from "@/api/orders/salecoupondata.js";
    import { enumList,urlList, } from "@/api/public.api.js";
    import * as ftVal from "@/common/fortressValidator.js";
    export default {
        data() {
            return {
                visible: false,
                form: {
                    memberId:"",
                    couponId:"",
                    getType:"",
                    useTime:"",
                    getTime:"",
                    orderSn:"",
                    effectiveTime:"",
                    overdueTime:"",
                    status:"",
                    payType:"",
                    payNumber:"",
                    pointNumber:"",
                },
                formLabelWidth: "120px",
                rules: {
                    memberId:[
                          { required: true, message: "请输入会员", trigger: "blur" },
                      ],
                    couponId:[
                          { required: true, message: "请输入优惠券", trigger: "blur" },
                      ],
                    payNumber:[
                          { validator: ftVal.validatePInteger,trigger: "blur"},
                      ],
                    pointNumber:[
                          { validator: ftVal.validatePInteger,trigger: "blur"},
                      ],
                },
                CouponGetType_ordersList:[],
                CouponStatus_ordersList:[],
                CouponPayType_ordersList:[],
                YesNoList:[],
                memberIdList:[],
                couponIdList:[],
            };
        },
        created() {
        },
        methods: {
            queryEnumList(){
                this.CouponGetType_ordersList = [];
                enumList("CouponGetType","orders").then((resp) => {
                  if (resp.code == 0) {
                    this.CouponGetType_ordersList = resp.data;
                  }
                });
                this.CouponStatus_ordersList = [];
                enumList("CouponStatus","orders").then((resp) => {
                  if (resp.code == 0) {
                    this.CouponStatus_ordersList = resp.data;
                  }
                });
                this.CouponPayType_ordersList = [];
                enumList("CouponPayType","orders").then((resp) => {
                  if (resp.code == 0) {
                    this.CouponPayType_ordersList = resp.data;
                  }
                });
                this.YesNoList = [];
                enumList("YesNo","").then((resp) => {
                  if (resp.code == 0) {
                    this.YesNoList = resp.data;
                  }
                });
            },
            queryUrlList(){
                this.memberIdList = [];
                urlList("member/member/list").then((resp) => {
                  if (resp.code == 0) {
                    if (resp.data) {
                      resp.data.forEach((item) => {
                        this.memberIdList.push({ code: item.id, name: item.nickName });
                      });
                    }
                  }
                });
                this.couponIdList = [];
                urlList("/orders/saleCoupon/list").then((resp) => {
                  if (resp.code == 0) {
                    if (resp.data) {
                      resp.data.forEach((item) => {
                        this.couponIdList.push({ code: item.id, name: item.couponName });
                      });
                    }
                  }
                });
            },
            init(id) {
                this.resetForm();
                this.queryEnumList();
                this.queryUrlList();
                //修改
                if (id) {
                    salecoupondataInfo(id).then((resp) => {
                        if (resp.code == 0) {
                            this.$nextTick(() => {
                                this.form = {
                                    ...this.form,
                                    ...resp.data,
                                };
                            });
                        } else {
                            this.$msg({
                                message: resp.msg,
                                type: "error",
                            });
                        }
                    });
                }
                this.visible = true;
            },
            //提交表单数据
            submitForm() {
                this.$refs["form"].validate((valid) => {
                if (valid) {
                    //修改
                    if (this.form.id) {
                        salecoupondataEdit(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                            this.$emit("refreshData");
                            this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    //新增
                    } else {
                        salecoupondataSave(this.form).then((resp) => {
                            if (resp.code == 0) {
                                this.$message({
                                    type: "success",
                                    message: "操作成功!",
                                });
                                this.$emit("refreshData");
                                this.visible = false;
                            } else {
                                this.$msg({
                                    message: resp.msg,
                                    type: "error",
                                });
                            }
                        });
                    }
                } else {
                    console.log("error submit!!");
                    return false;
                }
                });
            },
            resetForm() {
                this.form = {
                    memberId:"",
                    couponId:"",
                    getType:"",
                    useTime:"",
                    getTime:"",
                    orderSn:"",
                    effectiveTime:"",
                    overdueTime:"",
                    status:"",
                    payType:"",
                    payNumber:"",
                    pointNumber:"",
                };
                this.$nextTick(() => {
                    if (this.$refs["form"]) {
                        this.$refs["form"].resetFields();
                    }
                });
            },
        },
    };
</script>
<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    .el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
        height: auto;
        max-height: 274px;
        padding: 7px;
        overflow: hidden;
        overflow-y: auto;
        font-weight: normal;
    }
</style>
